//
//  LPPTabBarView.m
//  Lottery
//
//  Created by 刘盼 on 16/1/21.
//  Copyright © 2016年 lhheh. All rights reserved.
//

#import "LPPTabBarView.h"

@interface LPPTabBarView()

@property(nonatomic,weak)UIButton *currentBtn;
@end

@implementation LPPTabBarView
//添加按钮
-(void)addButtonWithImage:(NSString *)imageName imagePress:(NSString *)imagePressName{
    
    UIButton *btn = [[UIButton alloc]init];
    
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [btn setImage:[UIImage imageNamed:imagePressName] forState:UIControlStateSelected];
    
    [self addSubview:btn];
    
    [btn addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchDown];
}
//按钮点击跳转控制器
-(void)BtnClick:(UIButton *)button{
    
    self.currentBtn.selected = NO;
    
    button.selected = YES;
    
    self.currentBtn = button;
    
    if ([self.delegate respondsToSelector:@selector(tabBar:WithIndex:)]) {
       
        [self.delegate tabBar:self WithIndex:button.tag];
    }
    
}

-(void)layoutSubviews {
    
    [super layoutSubviews];
    
    for (int i = 0; i < self.subviews.count; i++) {
        
        CGFloat w = self.bounds.size.width / self.subviews.count;
        
        CGFloat h = self.bounds.size.height;
        
        CGFloat x = w * i;
        
        CGFloat y = 0;
        
        [self.subviews[i] setFrame:CGRectMake(x, y, w, h)];
        
        self.subviews[i].tag = i;
        
        if (i == 0) {
            
            [self BtnClick:self.subviews[0]];
        }
    }
}
@end
